<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\DB;
use think\Controller;
use think\Request;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Cash extends Backend
{
    
    /**
     * Cash模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Cash');

    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个方法
     * 因此在当前控制器中可不用编写增删改查的代码,如果需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */
    public function index()
    {
        if ($this->request->isAjax())
        {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            foreach($list as $key=>$val){

                $userinfo = Db::name('user')->where('userId',$val['userId'])->find();
                if($userinfo){
                    $list[$key]['name']   = $userinfo['name'];
                    $list[$key]['phone']   = $userinfo['phone'];
                }else{
                    $list[$key]['name']   = '';
                    $list[$key]['phone']   = '';
                }

            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
    public function detail($id = NULL)
    {
        $row = Db::name('cash')->where('id',$id)->find();
        $user = Db::name('user')->where('userId',$row['userId'])->find();
        $row['name']   = $user['name'];
        $row['phone']  = $user['phone'];
        $this->view->assign('row',$row);
        return $this->view->fetch();
    }

}
